<template>
  <div class="app-container">
    <el-page-header @back="goBack" >
    </el-page-header>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          v-if="columns[0].visible"
          label="领用单编码"
          min-width="200"
          align="center"
          prop="useNum"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          v-if="columns[3].visible"
          label="车间"
          min-width="180"
          align="center"
          prop="workshopName"
        />
        <el-table-column
          v-if="columns[3].visible"
          label="机台名称"
          min-width="180"
          align="center"
          prop="machineName"
        />
        <el-table-column
          v-if="columns[4].visible"
          label="出库日期"
          min-width="180"
          align="center"
          prop="outboundDate"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            {{ row.outboundDate ? row.outboundDate.split(" ")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="纸型"
          v-if="columns[5].visible"
          align="center"
          :show-overflow-tooltip="true"
          prop="paperType"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[6].visible"
          label="纸张编码"
          min-width="150"
          align="center"
          prop="code"
        ></el-table-column>
        <el-table-column
          v-if="columns[8].visible"
          label="纸张名称"
          min-width="180"
          align="center"
          prop="name"
        />
        <el-table-column
          v-if="columns[13].visible"
          min-width="120"
          label="领用数量"
          align="center"
          prop="useNumber"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[9].visible"
          label="规格"
          min-width="120"
          align="center"
          prop="specs"
        >
          <template #default="scope">
            <span v-if="scope.row.longWide">
              {{ scope.row.longWide }}
            </span>
            <span v-if="scope.row.specs">
              <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
            </span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[10].visible"
          min-width="120"
          label="品牌"
          align="center"
          prop="brand"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_brand" :value="scope.row.brand" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[11].visible"
          width="120"
          label="定量"
          align="center"
          prop="weight"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[2].visible"
          label="厂/客料"
          min-width="120"
          align="center"
          prop="material"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="mes_paper_material"
              :value="scope.row.material"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[12].visible"
          width="100"
          label="单位"
          align="center"
          prop="unit"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[14].visible"
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="order">
import { downLoadBlob } from "@/utils/ruoyi";
import useUserStore from "@/store/modules/user";
import {getUseDetail} from "@/api/storage/paperAccess.js";
import { getList as getWorkBenchList } from "@/api/basicInfo/workbench";
import { readonly } from "vue";
import { toNumber } from "@/utils/ruoyi";
import useTagsViewStore from '@/store/modules/tagsView'

const tagsViewStore = useTagsViewStore()
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const route = useRoute();
const total = ref(0);
const advanced = ref(false);
const useNumberSum = ref(0);
const workbranchList = ref([]);
const multiple = ref(true);
const selectRow = ref([]);
const statusOptions = ref([
  { value: "0", label: "全部" },
  { value: "1", label: "未领用" },
  { value: "2", label: "已领用" },
]);
//const   selected=ref("1");

// 列显隐信息
const columns = ref([
  { key: 0, label: `领用单编码`, visible: true },
  { key: 1, label: `生产单编码`, visible: true },
  { key: 2, label: `领用机台编码`, visible: true },
  { key: 3, label: `机台名称`, visible: true },
  { key: 4, label: `出库日期`, visible: true },
  { key: 5, label: `纸型`, visible: true },
  { key: 6, label: `纸张编码`, visible: true },
  { key: 7, label: `自有编码`, visible: true },
  { key: 8, label: `纸张名称`, visible: true },
  { key: 9, label: `规格型号`, visible: true },
  { key: 10, label: `品牌`, visible: true },
  { key: 11, label: `定量`, visible: true },
  { key: 12, label: `单位`, visible: true },
  { key: 13, label: `领用数量`, visible: true },
  { key: 14, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 30,
    status: "2",
  },
});
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "useNumber":
        sums[index] = useNumberSum.value;
        break;
      default:
        break;
    }
  });
  return sums;
};


const { queryParams } = toRefs(data);
const {
  mes_paper_type,
  mes_paper_specs,
  mes_paper_brand,
  mes_paper_unit,
  mes_paper_material,
} = proxy.useDict(
  "mes_paper_type",
  "mes_paper_specs",
  "mes_paper_brand",
  "mes_paper_unit",
  "mes_paper_material"
);

const getList = () => {
  loading.value = true;

  
  if(route.query?.paperId){
      queryParams.value.paperId=route.query.paperId;
      queryParams.value.startDate=route.query.startDate;
      queryParams.value.endDate=route.query.endDate;
      if(route.query.machineId!='5ce9c01407214812904ebbce9c405830'){
        queryParams.value.machineId=route.query.machineId;
      }else{
        queryParams.value.machineId='5ce9c01407214812904ebbce9c405830,325eb4a641ef4d5fb1d42d275d607e21';
      }
      
    }
  getUseDetail(queryParams.value)
    .then((res) => {
      loading.value = false;
      if (res.rows.length > 0) {
        res.rows[0].useNumberSum = 0;
        res.rows.forEach((row) => {
          res.rows[0].useNumberSum =
            res.rows[0].useNumberSum + Number(row.useNumber || 0);
        });
        useNumberSum.value = toNumber(res.rows[0].useNumberSum, 3);
      }
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};


const handleViewCode = (row) => {
  router.push({
    path: "/storage/paperAccess/accessDetails",
    query: {
      code: row.useNum,
      id: row.useinfoId,
      info: true,
    },
  });
};
// 查看纸张
const handleViewPaperCode = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      code: row.code,
      id: row.paperId,
      readonly: true,
    },
  });
};
// 查看机台
const handleViewMachineName = (row) => {
  router.push({
    path: "/basicInfo/workbench/details",
    query: {
      id: row.machineId,
      info: true,
    },
  });
};

  //  返回
  const goBack = () => {
    setTimeout(() => {
    router.go(-1);
    tagsViewStore.delView(route);
  }, 0);
  };

// 选中
const handleSelectionChange = (row) => {
  selectRow.value = row;
};


getList();
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
